/*
 ******************************************************************************
 * Parts of this code sample are licensed under Apache License, Version 2.0   *
 * Copyright (c) 2009, Android Open Handset Alliance. All rights reserved.    *
 *																			  *																			*
 * Except as noted, this code sample is offered under a modified BSD license. *
 * Copyright (C) 2010, Motorola Mobility, Inc. All rights reserved.           *
 * 																			  *
 * For more details, see MOTODEV_Studio_for_Android_LicenseNotices.pdf        * 
 * in your installation folder.                                               *
 ******************************************************************************
 */
package me.evis.mobile.sms2cal.provider;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class ParsedEventContentProvider extends ContentProvider {

    private Sms2CalDbHelper dbHelper;
    private static HashMap<String, String> PARSEDEVENT_PROJECTION_MAP;
    private static final String TABLE_NAME = "parsedevent";
    private static final String JOINED_TABLE_NAME = "parselog JOIN calevent ON parselog._id = calevent.parselog_id";
    private static final String AUTHORITY = "me.evis.mobile.sms2cal.provider.parsedeventcontentprovider";

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/" + TABLE_NAME);
    public static final Uri _ID_FIELD_CONTENT_URI = Uri.parse("content://"
            + AUTHORITY + "/" + TABLE_NAME.toLowerCase());

    public static final String DEFAULT_SORT_ORDER = "parselog_id DESC";

    private static final UriMatcher URL_MATCHER;

    private static final int PARSEDEVENT = 1;
    private static final int PARSEDEVENT__ID = 2;
//    private static final int PARSELOG_RECEIVE_DATE = 3;
//    private static final int PARSELOG_PARSE_DATE = 4;
//    private static final int PARSELOG_SMS_FROM = 5;
//    private static final int PARSELOG_SMS_BODY = 6;
//    private static final int PARSELOG_READ = 7;
//    private static final int CALEVENT_SUMMARY = 4;
//    private static final int CALEVENT_LOCATION = 5;
//    private static final int CALEVENT_DTSTAMP = 6;
//    private static final int CALEVENT_DTSTART = 7;
//    private static final int CALEVENT_DTEND = 8;
//    private static final int CALEVENT_DESCRIPTION = 9;
//    private static final int CALEVENT_CLASSIFICATION = 10;
//    private static final int CALEVENT_CATEGORIES = 11;
//    private static final int CALEVENT_TARGET_GCAL_ID = 12;

    // Content values keys (using column names)
    public static final String _ID = "_id";
    public static final String RECEIVE_DATE = "receive_date";
    public static final String PARSE_DATE = "parse_date";
    public static final String SMS_FROM = "sms_from";
    public static final String SMS_BODY = "sms_body";
    public static final String READ = "read";
    public static final String PARSELOG_ID = "parselog_id";
    public static final String SUMMARY = "summary";
    public static final String LOCATION = "location";
    public static final String DTSTAMP = "dtStamp";
    public static final String DTSTART = "dtStart";
    public static final String DTEND = "dtEnd";
    public static final String DESCRIPTION = "description";
    public static final String CLASSIFICATION = "classification";
    public static final String CATEGORIES = "categories";
    public static final String TARGET_GCAL_ID = "target_gcal_id";

    public boolean onCreate() {
        dbHelper = new Sms2CalDbHelper(getContext(), true);
        return (dbHelper == null) ? false : true;
    }

    public Cursor query(Uri url, String[] projection, String selection,
            String[] selectionArgs, String sort) {
        SQLiteDatabase mDB = dbHelper.getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        switch (URL_MATCHER.match(url)) {
        case PARSEDEVENT:
            qb.setTables(JOINED_TABLE_NAME);
            qb.setProjectionMap(PARSEDEVENT_PROJECTION_MAP);
            break;
        case PARSEDEVENT__ID:
            qb.setTables(JOINED_TABLE_NAME);
            qb.appendWhere("parselog_id=" + url.getPathSegments().get(1));
            break;

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }
        String orderBy = "";
        if (TextUtils.isEmpty(sort)) {
            orderBy = DEFAULT_SORT_ORDER;
        } else {
            orderBy = sort;
        }
        Cursor c = qb.query(mDB, projection, selection, selectionArgs, null,
                null, orderBy);
        c.setNotificationUri(getContext().getContentResolver(), url);
        return c;
    }

    public String getType(Uri url) {
        switch (URL_MATCHER.match(url)) {
        case PARSEDEVENT:
            return "vnd.android.cursor.dir/vnd.me.evis.mobile.sms2cal.provider.parsedevent";
        case PARSEDEVENT__ID:
            return "vnd.android.cursor.item/vnd.me.evis.mobile.sms2cal.provider.parsedevent";

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }
    }

    public Uri insert(Uri url, ContentValues initialValues) {
        throw new UnsupportedOperationException("Insert operation is now unsupported for " + url);
    }

    public int delete(Uri url, String where, String[] whereArgs) {
        throw new UnsupportedOperationException("Delete operation is now unsupported for " + url);
    }

    public int update(Uri url, ContentValues values, String where,
            String[] whereArgs) {
        throw new UnsupportedOperationException("Update operation is now unsupported for " + url);
    }

    static {
        URL_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase(), PARSEDEVENT);
        URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/#",
                PARSEDEVENT__ID);

        PARSEDEVENT_PROJECTION_MAP = new HashMap<String, String>();
        PARSEDEVENT_PROJECTION_MAP.put(_ID, "parselog._id");
        PARSEDEVENT_PROJECTION_MAP.put(PARSELOG_ID, "calevent.parselog_id");
        PARSEDEVENT_PROJECTION_MAP.put(RECEIVE_DATE, "parselog.receive_date");
        PARSEDEVENT_PROJECTION_MAP.put(PARSE_DATE, "parselog.parse_date");
        PARSEDEVENT_PROJECTION_MAP.put(SMS_FROM, "parselog.sms_from");
        PARSEDEVENT_PROJECTION_MAP.put(SMS_BODY, "parselog.sms_body");
        PARSEDEVENT_PROJECTION_MAP.put(READ, "parselog.read");
        PARSEDEVENT_PROJECTION_MAP.put(SUMMARY, "calevent.summary");
        PARSEDEVENT_PROJECTION_MAP.put(LOCATION, "calevent.location");
        PARSEDEVENT_PROJECTION_MAP.put(DTSTAMP, "calevent.dtStamp");
        PARSEDEVENT_PROJECTION_MAP.put(DTSTART, "calevent.dtStart");
        PARSEDEVENT_PROJECTION_MAP.put(DTEND, "calevent.dtEnd");
        PARSEDEVENT_PROJECTION_MAP.put(DESCRIPTION, "calevent.description");
        PARSEDEVENT_PROJECTION_MAP.put(CLASSIFICATION, "calevent.classification");
        PARSEDEVENT_PROJECTION_MAP.put(CATEGORIES, "calevent.categories");
        PARSEDEVENT_PROJECTION_MAP.put(TARGET_GCAL_ID, "calevent.target_gcal_id");

    }
}
